package com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeChangePassword.persenter;

import android.content.Context;
import android.text.TextUtils;

import com.example.anew.journey.mynewjourney.base.MyApplication;
import com.example.anew.journey.mynewjourney.bean.TakeDateBean;
import com.example.anew.journey.mynewjourney.config.RBConstants;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeChangePassword.bean.ChangePasswordBean;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeChangePassword.model.FirstChangeChangePasswordModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeChangePassword.model.ISFirstChangeChangePasswordModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeChangePassword.view.ISFirstChangeChangePasswordView;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 杭州惟君医疗科技有限公司17
 * Created by 20433 on 2018/3/16
 */

public class FirstChangeChangePasswordPersenter {
    private ISFirstChangeChangePasswordView mISFirstChangeChangePasswordView;
    private ISFirstChangeChangePasswordModel mISFirstChangeChangePasswordModel ;
    public FirstChangeChangePasswordPersenter(ISFirstChangeChangePasswordView iSFirstChangeChangePasswordView) {
        this.mISFirstChangeChangePasswordView = iSFirstChangeChangePasswordView;
        mISFirstChangeChangePasswordModel = new FirstChangeChangePasswordModel();
    }
    //点击完成按钮
    public void  onClickBtComplete(Context mContext){
        TakeDateBean bean = mISFirstChangeChangePasswordModel.TakeDate(mContext);
        if(bean == null )return;
      String oldPassword =   mISFirstChangeChangePasswordView.getCurrentPassword();
       String newpasssword =  mISFirstChangeChangePasswordView.getNewPassword();
       String confirmPassword =  mISFirstChangeChangePasswordView.getConfirmPassword();
        if(TextUtils.isEmpty(oldPassword)) mISFirstChangeChangePasswordView.showToastInformation("请输入当前密码");
        if(TextUtils.isEmpty(newpasssword)) mISFirstChangeChangePasswordView.showToastInformation("请输入新密码");
        if(TextUtils.isEmpty(confirmPassword)) mISFirstChangeChangePasswordView.showToastInformation("请再次输入新密码");
        if(newpasssword.equals(confirmPassword)){
            submitChangePassword(bean.getUser_id(),oldPassword,newpasssword);
        }else{
            mISFirstChangeChangePasswordView.showToastInformation("输入密码不一致");
        }
       // mISFirstChangeChangePasswordView.onClickeComplete();

    }

    private void submitChangePassword(String user_id, String oldPassword, String newpasssword) {
        String url = RBConstants.SERVER_URL+RBConstants.CHANGE_PASSWORD;
        OkHttpUtils
                .post()
                .url(url)
                .addParams("cus_id", user_id)
                .addParams("old_password", oldPassword)
                .addParams("new_password", newpasssword)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson = new Gson();
                          //NLogger.e("TAG","============NetworkResponseDate================="+NetworkResponseDate);
                        final ChangePasswordBean changePasswordBean =  gson.fromJson(NetworkResponseDate, ChangePasswordBean.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(changePasswordBean.errorCode == 200 ){
                                    mISFirstChangeChangePasswordView.showToastInformation("密码修改成功");
                                    mISFirstChangeChangePasswordView.onClickeComplete();
                                }else{
                                    mISFirstChangeChangePasswordView.showToastInformation("密码修改失败");
                                }
                            }
                        });

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                     //   mISFirstChangeChangePasswordView.showToastInformation("请查看网络");
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
}
